package com.dgut.service.serviceImpl;

import cn.dev33.satoken.stp.StpUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.dgut.common.*;
import com.dgut.mapper.LegalRecourseMapper;
import com.dgut.mapper.OrderFlagMapper;
import com.dgut.mapper.OrderGrabbingMapper;
import com.dgut.mapper.ServiceProviderMapper;
import com.dgut.pojo.LegalRecourse;
import com.dgut.pojo.OrderFlag;
import com.dgut.pojo.OrderGrabbing;
import com.dgut.service.LegalRecourseService;
import com.dgut.service.NoticeService;
import com.dgut.utils.PageRequestInfo;
import com.dgut.utils.PageResult;
import com.dgut.utils.PageUtils;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import java.util.Date;
import java.util.concurrent.ConcurrentHashMap;

@Service
public class LegalRecourseServiceImpl implements LegalRecourseService {
    @Resource
    private LegalRecourseMapper legalRecourseMapper;
    @Resource
    private OrderGrabbingMapper orderGrabbingMapper;
    @Resource
    private OrderFlagMapper orderFlagMapper;
    @Resource
    private ServiceProviderMapper serviceProviderMapper;
    @Resource
    private NoticeService noticeService;
    /**
     * start-zwb
     */
    //采买方获取法律求助列表
    public Object getLegalRecourseListToBuyer(PageRequestInfo pageRequestInfo,LegalRecourse legalRecourse)
    {
        Page<LegalRecourse> page = new Page<>(pageRequestInfo.getPageNum(),pageRequestInfo.getPageSize());
        QueryWrapper<LegalRecourse> queryWrapper = new QueryWrapper<>();
        Integer id = Integer.valueOf((String) StpUtil.getLoginId());
            queryWrapper.eq("buyer_id",id);
        if(legalRecourse.getTitle()!=null)
            queryWrapper.like("title",legalRecourse.getTitle());
        if(legalRecourse.getState()!=null)
        queryWrapper.like("state",legalRecourse.getState());
        queryWrapper.orderByDesc("initiation_time");
        Page<LegalRecourse> resultPage = legalRecourseMapper.selectPage(page, queryWrapper);
        PageResult result = PageUtils.getPageResult(resultPage);
        return result;
    }
    //服务商获取法律求助列表
    public Object getLegalRecourseListToServiceProvider(PageRequestInfo pageRequestInfo,String title,String state)
    {
        Page<LegalRecourse> page = new Page<>(pageRequestInfo.getPageNum(),pageRequestInfo.getPageSize());
        QueryWrapper<LegalRecourse> queryWrapper = new QueryWrapper<>();
        QueryWrapper<OrderGrabbing> queryWrapperTwo = new QueryWrapper<>();
        if(title!=null)
            queryWrapper.like("title",title);
        Integer id = Integer.valueOf((String) StpUtil.getLoginId());
        if("condition1".equals(state))//抢单中自己还没抢的单
        {
              queryWrapper.notInSql("id","select legal_recourse_id from order_grabbing where service_id = '"+id+"'");
              queryWrapper.like("state",StateForLegalRecourse.IN_ORDER_GRABBING);
        }
        else if("condition2".equals(state))//抢单中自己已抢的单
        {
              queryWrapper.inSql("id","select legal_recourse_id from order_grabbing where service_id = '"+id+"'");
              queryWrapper.like("state",StateForLegalRecourse.IN_ORDER_GRABBING);
        }
        else if("condition3".equals(state))//已被自己接单的法律求助
        {
            queryWrapper.like("state",StateForLegalRecourse.RECEIVED);
            queryWrapper.eq("service_id",id);
        }
        else if("condition4".equals(state))//自己抢单且已被他人接单的法律求助
        {
            queryWrapper.inSql("id","select legal_recourse_id from order_grabbing where service_id = '"+id+"'");
            queryWrapper.like("state",StateForLegalRecourse.RECEIVED);
            queryWrapper.ne("service_id",id);
        }
        queryWrapper.orderByDesc("initiation_time");
        Page<LegalRecourse> resultPage = legalRecourseMapper.selectPage(page, queryWrapper);
        PageResult result = PageUtils.getPageResult(resultPage);
        return result;
    }
    //根据法律求助id获取抢单表
    public Object getOrderGrabbingList(PageRequestInfo pageRequestInfo,Integer id)
    {
        QueryWrapper<OrderGrabbing> queryWrapper = new QueryWrapper<>();
        queryWrapper.orderByDesc("order_grabbing_time");
        queryWrapper.eq("legal_recourse_id",id);
        return orderGrabbingMapper.selectList(queryWrapper);
    }
    //添加抢单
    public Object grabOrder(OrderGrabbing orderGrabbing)
    {
        ConcurrentHashMap resultMap = new ConcurrentHashMap();
        if(orderGrabbingMapper.insert(orderGrabbing)>0)
        {
            LegalRecourse legalRecourse = legalRecourseMapper.selectById(orderGrabbing.getLegalRecourseId());
            legalRecourse.setOrderGrabbingNumber(legalRecourse.getOrderGrabbingNumber()+1);
            legalRecourseMapper.updateById(legalRecourse);
            resultMap.put("message","更新成功");
            resultMap.put("result",true);


            /**
             * 抢单通知
             */
            if(legalRecourse != null){
                noticeService.orderRequestNotice(legalRecourse.getBuyerId(),legalRecourse.getId(),orderGrabbing.getServiceId());
            }



        }
        else {
            resultMap.put("message","更新失败");
            resultMap.put("result",false);
        }

        return resultMap;
    }
    //选择服务商
    public Object selectServiceProvider(Integer id)
    {
        ConcurrentHashMap resultMap = new ConcurrentHashMap();
        OrderGrabbing orderGrabbing = orderGrabbingMapper.selectById(id);
        Integer legalRecourseId = orderGrabbing.getLegalRecourseId();
        Integer serviceId = orderGrabbing.getServiceId();
        Double money =orderGrabbing.getMoney();
        Integer buyerId = Integer.valueOf((String) StpUtil.getLoginId());
        OrderFlag orderFlag = new OrderFlag();
        orderFlag.setServiceId(serviceId);
        orderFlag.setBuyerId(buyerId);
        orderFlag.setType(OrderType.LEGAL_RECOURSE);
        orderFlag.setTypeId(legalRecourseId);
        orderFlag.setCreateTime(new Date());
        orderFlag.setStateTime(new Date());
        orderFlag.setState(StateForOrder.WAITING_FOR_PAYMENT);
        orderFlag.setMoney(money);
        orderFlag.setPaymentOrNot(OnOrOff.OFF);
        orderFlag.setExceptionIdentification(ExceptionIdentificationForOrder.NORMAL);
        LegalRecourse legalRecourse = legalRecourseMapper.selectById(legalRecourseId);
        String enterpriseName = serviceProviderMapper.selectById(serviceId).getEnterpriseName();
        legalRecourse.setMoney(money);
        legalRecourse.setServiceId(serviceId);
        legalRecourse.setEnterpriseName(enterpriseName);
        legalRecourse.setState(StateForLegalRecourse.RECEIVED);
        legalRecourseMapper.updateById(legalRecourse);
        if(orderFlagMapper.insert(orderFlag)>0)
        {
            resultMap.put("message","更新成功");
            resultMap.put("result",true);

            /**
             * 接单结果通知
             */
            noticeService.resultOfOrderGrabNotice(legalRecourseId,serviceId);

        }
        else {
            resultMap.put("message","更新失败");
            resultMap.put("result",false);
        }



        return resultMap;
    }
    //根据id获取法律求助
    public Object getLegalRecourseById(Integer id)
    {
        return legalRecourseMapper.selectById(id);
    }
    //添加法律求助
    public Object addLegalRecourse(LegalRecourse legalRecourse)
    {
        ConcurrentHashMap resultMap = new ConcurrentHashMap();
        if(legalRecourseMapper.insert(legalRecourse)>0)
        {
            resultMap.put("message","更新成功");
            resultMap.put("result",true);
        }
        else {
            resultMap.put("message","更新失败");
            resultMap.put("result",false);
        }
        return resultMap;
    }
    //修改法律求助
    public Object modifyLegalRecourse(LegalRecourse legalRecourse)
    {
        ConcurrentHashMap resultMap = new ConcurrentHashMap();
        if(legalRecourseMapper.updateById(legalRecourse)>0)
        {
            resultMap.put("message","更新成功");
            resultMap.put("result",true);
        }
        else {
            resultMap.put("message","更新失败");
            resultMap.put("result",false);
        }
        return resultMap;
    }
    //删除法律求助
    public Object deleteLegalRecourse(Integer id)
    {
        ConcurrentHashMap resultMap = new ConcurrentHashMap();
        if(legalRecourseMapper.deleteById(id)>0)
        {
            resultMap.put("message","更新成功");
            resultMap.put("result",true);
        }
        else {
            resultMap.put("message","更新失败");
            resultMap.put("result",false);
        }
        return resultMap;
    }
    //发布法律求助
    public Object putOnLegalRecourse(Integer id)
    {
        ConcurrentHashMap resultMap = new ConcurrentHashMap();
        QueryWrapper<LegalRecourse> queryWrapper = new QueryWrapper<>();
        LegalRecourse legalRecourse = legalRecourseMapper.selectById(id);
        if(legalRecourse!=null)
        {
            legalRecourse.setState(StateForLegalRecourse.IN_ORDER_GRABBING);
            queryWrapper.eq("id",legalRecourse.getId());
            if(legalRecourseMapper.update(legalRecourse,queryWrapper)>0)
            {
                resultMap.put("message","更新成功");
                resultMap.put("result",true);
            }
            else {
                resultMap.put("message", "更新失败");
                resultMap.put("result", false);
            }
        }
        return resultMap;
    }
    //取消发布法律求助
    public Object takeDownLegalRecourse(Integer id)
    {
        ConcurrentHashMap resultMap = new ConcurrentHashMap();
        QueryWrapper<LegalRecourse> queryWrapper = new QueryWrapper<>();
        LegalRecourse legalRecourse = legalRecourseMapper.selectById(id);
        if(legalRecourse!=null)
        {
            legalRecourse.setState(StateForLegalRecourse.WAITING_FOR_Released);
            queryWrapper.eq("id",legalRecourse.getId());
            if(legalRecourseMapper.update(legalRecourse,queryWrapper)>0)
            {
                resultMap.put("message","更新成功");
                resultMap.put("result",true);
            }
            else {
                resultMap.put("message", "更新失败");
                resultMap.put("result", false);
            }
        }
        return resultMap;
    }
    /**
     * end-zwb
     */


    /**
     * start-gxd
     */
    /**
     * end-gxd
     */


    /**
     * start-lzh
     */
    /**
     * end-lzh
     */

    /**
     * start-hwf
     */
    /**
     * end-hwf
     */
}
